﻿
using System;
using System.Collections.Generic ;

using MskEdit.Msk.Model ;


namespace MskEdit.Msk.Frontend {
    
    public class MskFieldRadioCollection  {

        #region Private data
        
        private MskFieldUserControl                         anySelectedMskFieldUserControl_ ; 
        private Dictionary<MskLineKey, MskFieldUserControl> mskFieldUserControlDictionary_ ;       
            
        #endregion
        
        #region Private operations
        
        
        private void setCurrentSelection( MskFieldUserControl mskFieldUserControl ) {
           mskFieldUserControl.IsSelected = true ;
           anySelectedMskFieldUserControl_ = mskFieldUserControl ;
        }
        
        
        private void updateCurrentSelection( MskFieldUserControl mskFieldUserControl ) {
            
            clearCurrentSelection() ;
            setCurrentSelection( mskFieldUserControl ) ;
            
            if ( SelectionChanged != null )
                SelectionChanged.Invoke( mskFieldUserControl.MskLineKey ) ;
        }
        
        #endregion
        
        #region Private event handlers
        
        private void handleMskFieldUserControlRadioClick( MskFieldUserControl mskFieldUserControl ) {
            updateCurrentSelection( mskFieldUserControl ) ;
        }
        
        #endregion
        
        #region Public event declarations

        public delegate void SelectionChangedEventHandler( MskLineKey mskLineKey ) ;
        public event SelectionChangedEventHandler SelectionChanged ;

        #endregion

        #region Public foundation
    
        public MskFieldRadioCollection() {
            mskFieldUserControlDictionary_ = new Dictionary<MskLineKey, MskFieldUserControl>() ;
        }
    
        #endregion
        
        #region Public operations
        
        public void Add( MskLineKey mskLineKey, MskFieldUserControl mskFieldUserControl ) {
            mskFieldUserControlDictionary_.Add( mskLineKey, mskFieldUserControl ) ;
            mskFieldUserControl.RadioClick += handleMskFieldUserControlRadioClick ;
        }
        
        
        public void clearCurrentSelection() {
        
            if ( anySelectedMskFieldUserControl_ != null ) {
                anySelectedMskFieldUserControl_.IsSelected = false ;
                anySelectedMskFieldUserControl_ = null ;
            }            
        }
        
        #endregion
        
        #region Public Properties
        
        public MskFieldUserControl AnySelectedMskFieldUserControl {
            get { return anySelectedMskFieldUserControl_ ; }
        }
        
        
        public Dictionary<MskLineKey, MskFieldUserControl> Dictionary {
            get { return mskFieldUserControlDictionary_ ; }
        }
        
        #endregion
        
    }
}
